package com.web.Servlet;


import com.Service.UserService;
import com.Untils.SqlSessionFactoryUtils;
import com.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.InputStream;

@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
    private UserService service=new UserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //首先解决中文乱码问题
        request.setCharacterEncoding("UTF-8");

        //1.获取表单信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String gender = request.getParameter("Gender");
        String addr = request.getParameter("addr");

        //2.定义User
        User u=new User();
        u.setUsername(username);
        u.setPassword(password);
        u.setGender(gender);
        u.setAddr(addr);

        User user = service.selectById(username);

        if(user!=null)
        {
            //说明用户存在

            String path = request.getContextPath();

            request.setAttribute("Login_msg","用户名已存在！");

            response.getWriter().write("用户名已存在！");
            request.getRequestDispatcher(path+"/Login.jsp");

        }else {

            service.add(u);

            String path = request.getContextPath();

            response.sendRedirect(path+"/Login.jsp");
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
